<?php
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                                 |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: xiangminghu@clcw.com.cn                                        |
// +------------------------------------------------------------------------+
//

namespace AuctionBack\Logic;


use AuctionCore\Account\Back\Employee;

class QuestionOrderLogic extends BaseLogic
{
    protected $autoCheckFields = false;

    protected $questionOrderModel;

    protected $clcwUserLogic;

    public function _initialize()
    {
        $this->questionOrderModel = D('QuestionOrder', 'Model');

        $this->clcwUserLogic = D('ClcwUser', 'Logic');
    }

    /**
     * 付费提问订单列表
     *
     * @param int   $page
     * @param int   $size
     * @param array $params
     */
    public function getQuestionOrderList($page = 1, $size = 10, $params = [])
    {
        $map = [];
        if ($params['status']) {
            $map['status'] = ['eq', $params['status']];
        }
        if ($params['order_id']) {
            $map['order_sn'] = ['eq', $params['order_id']];
        }
        if ($params['pay_id']) {
            $map['pay_sn'] = ['eq', $params['pay_id']];
        }
        if ($params['pay_type']) {
            $map['pay_code'] = ['eq', $params['pay_type']];
        }
        if ($params['question_id']) {
            $map['question_id'] = ['eq', $params['question_id']];
        }
        if ($params['user_id']) {
            $map['user_id'] = ['eq', $params['user_id']];
        }
        if ($params['user_id']) {
            $map['user_id'] = ['eq', $params['user_id']];
        }
        if ($params['user_name']) {
            $userId = $this->clcwUserLogic->getUserIdByNickName($params['user_name']);
            $map['user_id'] = ['eq', $userId];
        }

        if ($params['start_time']) {
            $map['create_time'] = ['egt', strtotime($params['start_time'] . ' 00:00:00')];
        }
        if ($params['end_time']) {
            $map['create_time'] = ['elt', strtotime($params['end_time'] . ' 23:59:59')];
        }
        if ($params['start_time'] && $params['end_time']) {
            $map['create_time'] = [['egt', strtotime($params['start_time'] . ' 00:00:00')], ['elt', strtotime($params['end_time'] . ' 23:59:59')]];
        }
        if ($params['status'] == 3) {
            if ($params['return_start_time']) {
                $map['return_time'] = ['egt', strtotime($params['return_start_time'] . ' 00:00:00')];
            }
            if ($params['return_end_time']) {
                $map['return_time'] = ['elt', strtotime($params['return_end_time'] . ' 23:59:59')];
            }
            if ($params['return_start_time'] && $params['return_end_time']) {
                $map['return_time'] = [['egt', strtotime($params['return_start_time'] . ' 00:00:00')], ['elt', strtotime($params['return_end_time'] . ' 23:59:59')]];
            }
            $order = 'return_time DESC';
        }
        $cols = 'id,order_sn,pay_sn,pay_code,order_amount,question_id,user_id,status,create_time,return_time';
        $order = 'id DESC';
        $total = $this->questionOrderModel->field($cols)
            ->where($map)->count();
        $list = $this->questionOrderModel->field($cols)
            ->where($map)
            ->page($page)
            ->limit($size)
            ->order($order)
            ->select();
        if (!empty($list)) {
            foreach ($list as $k => $v) {
                $userInfo = $this->clcwUserLogic->getClcwUserInfo($v['user_id']);
                $list[$k]['pay_type'] = C('QUESTION_PAY_TYPE')[$v['pay_code']];
                $list[$k]['user_name'] = $userInfo['nick_name'];
                $list[$k]['return_time'] = date("Y-m-d H:i:s", $v['return_time']);
                $list[$k]['create_time'] = date("Y-m-d H:i:s", $v['create_time']);
            }
        }
        $result = [
            'list'  => $list,
            'total' => $total
        ];
        return $result;
    }


    /**
     * 提问付费订单详情
     *
     * @param int $id
     */
    public function getQuestionOrderInfo($id = 0)
    {
        $map['id'] = ['eq', $id];
        $cols = 'id,order_sn,pay_sn,pay_no,pay_code,order_amount,question_id,user_id,
                 emp_id,status,create_time,return_time';
        $res = $this->questionOrderModel->field($cols)
            ->where($map)
            ->find();
        $userInfo = $this->clcwUserLogic->getClcwUserInfo($res['user_id']);
        $res['user_name'] = $userInfo['nick_name'];
        $res['pay_type'] = C('QUESTION_PAY_TYPE')[$res['pay_code']];
        $employee = Employee::user_info_id($res['emp_id']);
        $res['emp_name'] = $employee['real_name'];
        $res['status_name'] = C('QUESTION_STATUS_NAME')[$res['status']];
        $res['create_time'] = date("Y-m-d H:i:s", $res['create_time']);
        $res['return_time'] = date("Y-m-d H:i:s", $res['return_time']);
        return $res;
    }

    /**
     * 提问付费订单退款操作
     *
     * @param array $data
     */
    public function questionrderefund($data = [])
    {
        if (empty($data) || empty($data['id'])) {
            return false;
        }
        $ret1 = $this->questionOrderModel->save($data);
        return $ret1;
    }

    /**
     * 根据$orderSn 获取信息
     *
     * @param string $orderSn
     */
    public function getQuestionOrderByOrderSn($orderSn = '')
    {
        if (empty($orderSn)) {
            return false;
        }
        $condition = [];
        $condition['order_sn'] = ['eq', $orderSn];
        return $this->questionOrderModel->where($condition)->find();
    }

    /**
     * 更新提问订单信息
     *
     * @param array $order
     */
    public function updateQuestionOrder($order = [])
    {
        if (empty($order) || empty($order['id'])) {
            return false;
        }
        $order['update_time'] = time();
        return $this->questionOrderModel->save($order);
    }

}